﻿package game 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import game.events.EventData;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class VolumeController extends MovieClip	{
		
		public var scroll_mc:MovieClip;
		public var bg_mc:MovieClip;
		public var mask_mc:MovieClip;
		public var round1_mc:MovieClip;
		public var round2_mc:MovieClip;
		public var round3_mc:MovieClip;
		
		private var rec:Rectangle;
		
		public function VolumeController() {	
			rec = new Rectangle( this.scroll_mc.x, 0, 0, 59);
			this.scroll_mc.buttonMode = true;
			
			this.scroll_mc.addEventListener(MouseEvent.MOUSE_DOWN, hdlScrollDown);			
			this.scroll_mc.addEventListener(MouseEvent.MOUSE_UP, hdlScrollUp);		
			this.scroll_mc.addEventListener(MouseEvent.MOUSE_MOVE, hdlScrollMove);
			this.scroll_mc.addEventListener(MouseEvent.MOUSE_OUT, hdlScrollUp);
		}
		
		private function hdlScrollDown(e:MouseEvent):void {						
			this.scroll_mc.startDrag(true, rec);
		}
		
		private function hdlScrollUp(e:MouseEvent):void {				
			this.scroll_mc.stopDrag();
			this.mask_mc.y = this.scroll_mc.y;
			var temp:Number = Number( 100 - (this.scroll_mc.y / this.bg_mc.height) * 100);
			roundSoundControl(temp);
			var event:EventData = new EventData(EventData.UPDATE_VOLUMEBACKGROUND);
			event.newBalance = temp;
			dispatchEvent(event);			
			
			
		}
		
		private function hdlScrollMove(e:MouseEvent):void {			
			this.mask_mc.y = this.scroll_mc.y;
		}	
		
		public function roundSoundControl(volume:Number) {
			if (volume > 70) {
				this.round1_mc.visible = true;
				this.round2_mc.visible = true;
				this.round3_mc.visible = true;
			} else if (volume > 35) {
				this.round1_mc.visible = false;
				this.round2_mc.visible = true;
				this.round3_mc.visible = true;
			} else if (volume > 0) {
				this.round1_mc.visible = false;
				this.round2_mc.visible = false;
				this.round3_mc.visible = true;
			} else {
				this.round1_mc.visible = false;
				this.round2_mc.visible = false;
				this.round3_mc.visible = false;
			}
		}
		
	}

}